#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll rr() {
    ll s = 0, w = false, c = getchar();
    for (; !isdigit(c); c = getchar())
        w = w || c == '-';
    for (; isdigit(c); c = getchar())
        s = s * 10 + c - '0';
    return w ? -s : s;
}

// END OF HEADER

struct Stu {
    string no;
    int kd, grade;
    int rank, kdrank;
    bool operator<(Stu &o) const {
        if (grade == o.grade)
            return no < o.no;
        return grade > o.grade;
    }
    int &getRank(bool b) {
        if (b)
            return rank;
        return kdrank;
    }
};

void getRank(vector<Stu> &v, bool b) {
    for (int i = 0; i < v.size(); i++) {
        int rank = i + 1;
        if (i > 0 && v[i].grade == v[i - 1].grade)
            rank = v[i - 1].getRank(b);
        v[i].getRank(b) = rank;
    }
}

int main() {
    int N = rr();
    vector<Stu> v;
    for (int i = 0; i < N; i++) {
        int K = rr();
        vector<Stu> kd;
        for (int j = 0; j < K; j++) {
            string no;
            cin >> no;
            int grade = rr();
            kd.push_back({no, i, grade});
        }
        sort(kd.begin(), kd.end());
        getRank(kd, false);
        v.insert(v.end(), kd.begin(), kd.end());
    }

    sort(v.begin(), v.end());
    getRank(v, true);

    printf("%zu\n", v.size());
    for (int i = 0; i < v.size(); i++) {
        printf("%s %d %d %d\n", v[i].no.data(), v[i].rank, v[i].kd + 1, v[i].kdrank);
    }

    return 0;
}
